<?php 
/**
* Test adding existing and uploaded file.
*/
class DomArChangeLogTestCase extends DomArTestUnitTestCase {
	
	public function testBasics() {
		
		DomArChangeLogTestClass_Item::getTable()->update();
		
		$u1 = new DomArChangeLogTestClass_User();
		$u1->username = 'Balduran';
		
		$u2 = new DomArChangeLogTestClass_User();
		$u2->username = 'Oliver';
		
		$a = new DomArChangeLogTestClass_Item();
		$a->datetime = '2009-10-10 00:00:00';
		$a->title  = 'Hello';
		$a->description = 'World';
		$a->user = $u1;
		
		$this->assertTrue($a->save(), $a->getValidationMessages('<br>'));
		
		// notice that insert order is reversed.
		$log = $a->changeLog->query()->orderBy('t.id DESC')->limit(4)->commit()->toArray();
		
		$this->assertIdentical( $log[0]->value, (string)$u1->id);
		$this->assertIdentical( $log[0]->propertyName, 'user');
		
		$this->assertIdentical( $log[1]->value, 'World');
		$this->assertIdentical( $log[1]->propertyName, 'description');
		
		$this->assertIdentical( $log[2]->value, 'Hello');
		$this->assertIdentical( $log[2]->objectId, $a->id);
		$this->assertIdentical( $log[2]->objectClassName, 'DomArChangeLogTestClass_Item');
		$this->assertIdentical( $log[2]->propertyName, 'title');
		
		$this->assertIdentical( $log[3]->value, '2009-10-10 00:00:00');
		$this->assertIdentical( $log[3]->propertyName, 'datetime');
		
		
		// set same value - it should not log that.
		$a->datetime = '2009-10-10 00:00:00';
		$a->title  = 'Hello';
		$a->description = 'World';
		$a->user = $u1;
		$a->save();
		
		// last log for user change.
		$nLog = $a->changeLog->query()->orderBy('t.id DESC')->limit(4)->commit()->toArray();
		$this->assertIdentical( $nLog[0]->id, $log[0]->id);
		$this->assertIdentical( $nLog[1]->id, $log[1]->id);
		$this->assertIdentical( $nLog[2]->id, $log[2]->id);
		$this->assertIdentical( $nLog[3]->id, $log[3]->id);
		
		
		
		
		
		
		$a->title = 'New title';
		$a->description  = 'New description';
		$a->save();
		
		$log = $a->changeLog->query()->orderBy('t.id DESC')->limit(2)->commit()->toArray();
		$this->assertIdentical( $log[0]->value, 'New description');
		$this->assertIdentical( $log[0]->propertyName, 'description');
		
		$this->assertIdentical( $log[1]->value, 'New title');
		$this->assertIdentical( $log[1]->objectId, $a->id);
		$this->assertIdentical( $log[1]->objectClassName, 'DomArChangeLogTestClass_Item');
		$this->assertIdentical( $log[1]->propertyName, 'title');
	
		
		
		
		$a->user = $u2;
		$a->save();
		$log = $a->changeLog->query()->orderBy('t.id DESC')->limit(1)->commit();
		$this->assertIdentical( $log->value, (string)$u2->id);
		$this->assertIdentical( $log->propertyName, 'user');
		
	}
	
	public function testExtended( ) {
		DomArChangeLogTestClass_ExtItem::getTable()->update();
		
		$u1 = new DomArChangeLogTestClass_User();
		$u1->username = 'Balduran';
		
		$u2 = new DomArChangeLogTestClass_User();
		$u2->username = 'Oliver';
		
		$a = new DomArChangeLogTestClass_ExtItem();
		$a->info = 'INFO';
		$a->datetime = '2009-10-10 00:00:00';
		$a->title  = 'Hello';
		$a->description = 'World';
		$a->user = $u1;
		
		$this->assertTrue($a->save(), $a->getValidationMessages('<br>'));
		
		// notice that insert order is reversed.
		$log = $a->changeLog->query()->orderBy('t.id DESC')->limit(5)->commit()->toArray();

		$this->assertIdentical( $log[0]->value, (string)$u1->id);
		$this->assertIdentical( $log[0]->propertyName, 'user');
		
		$this->assertIdentical( $log[1]->value, 'World');
		$this->assertIdentical( $log[1]->propertyName, 'description');
		
		$this->assertIdentical( $log[2]->value, 'Hello');
		$this->assertIdentical( $log[2]->objectId, $a->id);
		$this->assertIdentical( $log[2]->objectClassName, 'DomArChangeLogTestClass_ExtItem');
		$this->assertIdentical( $log[2]->propertyName, 'title');
		
		$this->assertIdentical( $log[3]->value, '2009-10-10 00:00:00');
		$this->assertIdentical( $log[3]->propertyName, 'datetime');
		
		$this->assertIdentical( $log[4]->value, 'INFO');
		$this->assertIdentical( $log[4]->propertyName, 'info');
		
		
		$a->user = $u2;
		$a->info = 'New info';
		$a->title = 'New title';
		$a->description  = 'New description';
		$a->save();
		
		$log = $a->changeLog->query()->orderBy('t.id DESC')->limit(4)->commit()->toArray();
		$this->assertIdentical( $log[0]->value, 'New description');
		$this->assertIdentical( $log[0]->propertyName, 'description');
		
		$this->assertIdentical( $log[1]->value, 'New title');
		$this->assertIdentical( $log[1]->objectId, $a->id);
		$this->assertIdentical( $log[1]->objectClassName, 'DomArChangeLogTestClass_ExtItem');
		$this->assertIdentical( $log[1]->propertyName, 'title');
	
		$this->assertIdentical( $log[2]->value, 'New info');
		$this->assertIdentical( $log[2]->propertyName, 'info');
		
		$this->assertIdentical( $log[3]->value, (string)$u2->id);
		$this->assertIdentical( $log[3]->propertyName, 'user');
		
	}

	
}
?>